import { _decorator, AnimationClip, Component, Animation, SpriteFrame } from 'cc';
import { ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, PARAMS_NAME_ENUM, SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM } from '../../Enums';
import { getInitParamsNumber, getInitParamsTrigger, StateMachine } from '../../Base/StateMachine';
import SpikeOneSubStateMachine from './SpikeOneSubStateMachine';
import SpikeTwoSubStateMachine from './SpikeTwoSubStateMachine';
import SpikeThreeSubStateMachine from './SpikeThreeSubStateMachine';
import SpikeFourSubStateMachine from './SpikeFourSubStateMachine';
import { SpikesManager } from './SpikesManager';


const { ccclass, property } = _decorator;


@ccclass('SpikesStateMachine')
export class SpikesStateMachine extends StateMachine {
    async init() {
        this.playerAnimation = this.addComponent(Animation);
        this.initParams()
        this.initStateMachine()
        this.initAnimationEvent()
        await Promise.all(this.waitingList)
    }

    initParams() {
        this.params.set(PARAMS_NAME_ENUM.SPIKES_CUR_COUNT, getInitParamsNumber())
        this.params.set(PARAMS_NAME_ENUM.SPIKES_TOTAL_COUNT, getInitParamsNumber())
    }

    initStateMachine() {
        this.stateMachine.set(ENTITY_TYPE_ENUM.SPIKES_ONE, new SpikeOneSubStateMachine(this))
        this.stateMachine.set(ENTITY_TYPE_ENUM.SPIKES_TWO, new SpikeTwoSubStateMachine(this))
        this.stateMachine.set(ENTITY_TYPE_ENUM.SPIKES_THREE, new SpikeThreeSubStateMachine(this))
        this.stateMachine.set(ENTITY_TYPE_ENUM.SPIKES_FOUR, new SpikeFourSubStateMachine(this))
    }

    initAnimationEvent() {
        this.playerAnimation.on(Animation.EventType.FINISHED, () => {
            const name = this.playerAnimation.defaultClip.name
            const value = this.getParams(PARAMS_NAME_ENUM.SPIKES_TOTAL_COUNT)

            if ((value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_ONE && name.includes('two')) ||
                (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_TWO && name.includes('three')) ||
                (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_THREE && name.includes('four')) ||
                (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_FOUR && name.includes('five'))) {
                this.node.getComponent(SpikesManager).count = 0
            }
        })
    }


    run() {
        const value = this.getParams(PARAMS_NAME_ENUM.SPIKES_TOTAL_COUNT)
        switch (this.currentState) {
            case this.stateMachine.get(ENTITY_TYPE_ENUM.SPIKES_ONE):
            case this.stateMachine.get(ENTITY_TYPE_ENUM.SPIKES_TWO):
            case this.stateMachine.get(ENTITY_TYPE_ENUM.SPIKES_THREE):
            case this.stateMachine.get(ENTITY_TYPE_ENUM.SPIKES_FOUR):
                if (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_ONE) {
                    this.currentState = this.stateMachine.get(ENTITY_TYPE_ENUM.SPIKES_ONE)
                } else if (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_TWO) {
                    this.currentState = this.stateMachine.get(ENTITY_TYPE_ENUM.SPIKES_TWO)
                } else if (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_THREE) {
                    this.currentState = this.stateMachine.get(ENTITY_TYPE_ENUM.SPIKES_THREE)
                } else if (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_FOUR) {
                    this.currentState = this.stateMachine.get(ENTITY_TYPE_ENUM.SPIKES_FOUR)
                } else {
                    this.currentState = this.currentState
                }
                break;
            default:
                this.currentState = this.stateMachine.get(ENTITY_TYPE_ENUM.SPIKES_ONE)
        }
    }
}


